import http from '@/http/index';

export interface StatisticsData {
  problem_count: number;
  contest_count: number;
  exam_count: number;
  question_count: number;
  user_count: number;
  submission_count: number;
  recent_activities: {
    id: number;
    type: string;
    title: string;
    created_at: string;
    user: {
      id: number;
      username: string;
    };
  }[];
  upcoming_events: {
    id: number;
    type: string;
    title: string;
    start_time: string;
    end_time: string;
  }[];
}

export const statisticsService = {
  getStatistics() {
    return http.get<{ data: StatisticsData }>('/api/statistics/');
  },
  
  // 模拟数据，用于开发阶段
  getMockStatistics(): Promise<{ data: { data: StatisticsData } }> {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: {
            data: {
              problem_count: 1250,
              contest_count: 45,
              exam_count: 32,
              question_count: 378,
              user_count: 1560,
              submission_count: 25678,
              recent_activities: [
                {
                  id: 1,
                  type: 'problem',
                  title: '用户 zhangsan 解决了题目 "两数之和"',
                  created_at: new Date(Date.now() - 1000 * 60 * 30).toISOString(),
                  user: {
                    id: 101,
                    username: 'zhangsan'
                  }
                },
                {
                  id: 2,
                  type: 'contest',
                  title: '比赛 "算法挑战赛" 已开始',
                  created_at: new Date(Date.now() - 1000 * 60 * 60 * 2).toISOString(),
                  user: {
                    id: 102,
                    username: 'admin'
                  }
                },
                {
                  id: 3,
                  type: 'submission',
                  title: '用户 lisi 提交了题目 "合并区间"',
                  created_at: new Date(Date.now() - 1000 * 60 * 60 * 5).toISOString(),
                  user: {
                    id: 103,
                    username: 'lisi'
                  }
                },
                {
                  id: 4,
                  type: 'exam',
                  title: '考试 "期中测试" 已发布',
                  created_at: new Date(Date.now() - 1000 * 60 * 60 * 12).toISOString(),
                  user: {
                    id: 102,
                    username: 'admin'
                  }
                },
                {
                  id: 5,
                  type: 'question',
                  title: '用户 wangwu 提问了 "如何优化动态规划算法？"',
                  created_at: new Date(Date.now() - 1000 * 60 * 60 * 24).toISOString(),
                  user: {
                    id: 104,
                    username: 'wangwu'
                  }
                }
              ],
              upcoming_events: [
                {
                  id: 1,
                  type: 'contest',
                  title: '每周算法挑战赛',
                  start_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 2).toISOString(),
                  end_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 2 + 1000 * 60 * 60 * 3).toISOString()
                },
                {
                  id: 2,
                  type: 'exam',
                  title: '期末考试',
                  start_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 5).toISOString(),
                  end_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 5 + 1000 * 60 * 60 * 2).toISOString()
                },
                {
                  id: 3,
                  type: 'contest',
                  title: '高级算法竞赛',
                  start_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7).toISOString(),
                  end_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7 + 1000 * 60 * 60 * 5).toISOString()
                },
                {
                  id: 4,
                  type: 'exam',
                  title: '算法基础测试',
                  start_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 10).toISOString(),
                  end_time: new Date(Date.now() + 1000 * 60 * 60 * 24 * 10 + 1000 * 60 * 60 * 1.5).toISOString()
                }
              ]
            }
          }
        });
      }, 800);
    });
  }
}; 